home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 270_01 / t3comp.c < prev    next >
Text File  |  1980-01-01  |  9KB  |  408 lines

  1. /*
  2.      HEADER:     CUG270.06 ;
  3.      TITLE:      TTT3D computer move subroutine ;
  4.      DATE:       06/08/1988 ;
  5.      VERSION:    1.0 ;
  6.      FILENAME:   T3COMP.C ;
  7.      AUTHORS:    Scott Holland ;
  8.      SEE-ALSO:   T3.DOC ;
  9. */
  10.  
  11. /* COPYRIGHT 1988  SCOTT HOLLAND */
  12.  
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include "t3global.h"
  16. #define P 0
  17. #define C 1
  18.  
  19. computer_move()
  20. {
  21.   int i,j,k,l,m,n,o,p,q,r,s ;
  22.   int sum, start ;
  23.   int sum_list[2][4][76];
  24.   if (game_over) return ;
  25.   for (i=0;i<4;i++)
  26.   {
  27.     for (j=0;j<2;j++) sum_count[j][i] = 0 ;
  28.   }
  29.   strategy = 15 ;
  30.   for (i=0;i<76;i++)
  31.   {
  32.     /* calculate sum of each potential winning row */
  33.     sum  = board[ win_square[i][0] ] +
  34.            board[ win_square[i][1] ] +
  35.        board[ win_square[i][2] ] +
  36.        board[ win_square[i][3] ] ;
  37.     switch (sum)
  38.     {
  39.       case 0 :
  40.       case 1 :
  41.       case 2 :
  42.       case 3 :
  43.     /* Player sums */
  44.     sum_list[P][sum][ sum_count[P][sum]++ ] = i ;
  45.     break ;
  46.       case 5 :
  47.       case 10 :
  48.       case 15 :
  49.     /* Computer sums */
  50.     sum = sum/5 ;
  51.     sum_list[C][sum][ sum_count[C][sum]++ ] = i ;
  52.     break ;
  53.       case 4 :
  54.     /* Player wins */
  55.     strategy = 1 ;
  56.     board[ win_square[i][0] ] = 2 ;
  57.     board[ win_square[i][1] ] = 2 ;
  58.     board[ win_square[i][2] ] = 2 ;
  59.     board[ win_square[i][3] ] = 2 ;
  60.     game_over = 1 ;
  61.     return ;
  62.     }
  63.   } /* End for loop */
  64.     
  65.   if (diag) printf("1");
  66.   
  67.   if ( sum_count[C][3] > 0 )
  68.   {
  69.     strategy = 2 ; /* Computer wins */
  70.     i = sum_list[C][3][0] ;
  71.     board[ win_square[i][0] ] = 2 ;
  72.     board[ win_square[i][1] ] = 2 ;
  73.     board[ win_square[i][2] ] = 2 ;
  74.     board[ win_square[i][3] ] = 2 ;
  75.     game_over = 1 ;
  76.     return ;
  77.   }
  78.  
  79.   if (diag) printf("2");
  80.   
  81.   for (i=0;i<sum_count[P][3];i++)
  82.   /* Computer checks for 3 in row for player */
  83.   {
  84.     k = sum_list[P][3][i] ;
  85.     for (j=0;j<4;j++) /* Look for empty square */
  86.     {
  87.       if ( board[ win_square[k][j] ] == 0 )
  88.       {
  89.     /* Block 3 in row for player */
  90.     move = win_square[k][j] ;
  91.     board[move] = 5 ;
  92.     strategy = 3 ;
  93.     return ;
  94.       }
  95.     }
  96.   } /* End i for loop */
  97.       
  98.   if (diag) printf("3");
  99.   
  100.   /* For computer */
  101.   /* Check if two intersecting rows with two in a row */
  102.   for (i=0;i<sum_count[C][2]-1;i++)
  103.   {
  104.     for (j=i+1;j<sum_count[C][2];j++)
  105.     {
  106.       /* 2 rows - 2 in row */
  107.       /* now find empty intersecting square */
  108.       k = sum_list[C][2][i] ; /* win_square index */
  109.       l = sum_list[C][2][j] ; /* win_square index */
  110.       for (m=0;m<4;m++)
  111.       {
  112.     if (board [ win_square[k][m] ] ==0 )
  113.     {
  114.       for (n=0;n<4;n++)
  115.       {
  116.         if (win_square[k][m]== win_square[l][n] )
  117.         {
  118.           move = win_square[k][m] ;
  119.           board[move] = 5;
  120.           strategy = 4;
  121.           return ;
  122.         }
  123.       }
  124.     }
  125.       }
  126.     }
  127.   }
  128.           
  129.   if (diag) printf("4");
  130.   
  131.           
  132.   /* For player */
  133.   /* Check if two intersecting rows with two in a row */
  134.   for (i=0;i<sum_count[P][2]-1;i++)
  135.   {
  136.     for (j=i+1;j<sum_count[P][2];j++)
  137.     {
  138.       /* 2 rows - 2 in row */
  139.       /* now find empty intersecting square */
  140.       k = sum_list[P][2][i] ; /* win_square index */
  141.       l = sum_list[P][2][j] ; /* win_square index */
  142.       for (m=0;m<4;m++)
  143.       {
  144.     if (board [ win_square[k][m] ] ==0 )
  145.     {
  146.       for (n=0;n<4;n++)
  147.       {
  148.         if (win_square[k][m]== win_square[l][n] )
  149.         {
  150.           move = win_square[k][m] ;
  151.           board[move] = 5;
  152.           strategy = 5;
  153.           return ;
  154.         }
  155.       }
  156.     }
  157.       }
  158.     }
  159.   }
  160.           
  161.   if (diag) printf("5");
  162.   
  163.           
  164.   /* For computer */
  165.   /* Check for a row with 2 intersecting
  166.      a row with 1 intersecting a different row with 2
  167.      with blank squares as intersecting squares */
  168.   for (i=0;i<sum_count[C][2]-1;i++)
  169.   {
  170.     for (j=i+1;j<sum_count[C][2];j++)
  171.     {
  172.       /* 2 rows - 2 in row */
  173.       k = sum_list[C][2][i] ; /* win_square index */
  174.       l = sum_list[C][2][j] ; /* win_square index */
  175.       for (m=0;m<sum_count[C][1];m++)
  176.       {
  177.     /* a row - 1 in row */
  178.     /* now find 2 empty intersecting squares */
  179.     n = sum_list[C][1][m] ; /* win_square index */
  180.     for (o=0;o<4;o++)
  181.     {
  182.       if (board [ win_square[n][o] ] ==0 )
  183.       {  /* square empty */
  184.         for (p=0;p<4;p++)
  185.         {
  186.           if (win_square[n][o]== win_square[l][p] )
  187.           { /* 2 intersects 1 in blank square */
  188.         for (q=0;q<4;q++)
  189.         {
  190.           if (board [ win_square[n][q] ] ==0 && q != p )
  191.           {  /* square empty in 1 in row */
  192.             for (r=0;r<4;r++)
  193.             {
  194.               if (win_square[n][q] == win_square[k][r] )
  195.               {
  196.              move = win_square[k][r] ;
  197.              move2 = win_square[l][p] ;
  198.              board[move] = 5;
  199.              strategy = 6;
  200.              return ;
  201.                }
  202.              }
  203.            }
  204.          }
  205.            }
  206.              }
  207.            }
  208.      }
  209.        }
  210.      }
  211.    }
  212.       
  213.    if (diag) printf("6");
  214.    
  215.  
  216.    /* For player */
  217.    /* Check for a row with 2 intersecting
  218.       a row with 1 intersecting a different row with 2
  219.       with blank squares as intersecting squares */
  220.    for (i=0;i<sum_count[P][2]-1;i++)
  221.    {
  222.      for (j=i+1;j<sum_count[P][2];j++)
  223.      {
  224.        /* 2 rows - 2 in row */
  225.        k = sum_list[P][2][i] ; /* win_square index */
  226.        l = sum_list[P][2][j] ; /* win_square index */
  227.        for (m=0;m<sum_count[P][1];m++)
  228.        {
  229.      /* a row - 1 in row */
  230.      /* now find 2 empty intersecting squares */
  231.      n = sum_list[P][1][m] ; /* win_square index */
  232.      for (o=0;o<4;o++)
  233.      {
  234.        if (board [ win_square[n][o] ] ==0 )
  235.        {  /* square empty */
  236.          for (p=0;p<4;p++)
  237.          {
  238.            if (win_square[n][o]== win_square[l][p] )
  239.            { /* 2 intersects 1 in blank square */
  240.          for (q=0;q<4;q++)
  241.          {
  242.            if (board [ win_square[n][q] ] ==0 && q != p )
  243.            {  /* square empty in 1 in row */
  244.               for (r=0;r<4;r++)
  245.               {
  246.             if (win_square[n][q] == win_square[k][r] )
  247.             {
  248.               move = win_square[k][r] ;
  249.               move2 = win_square[l][p] ;
  250.               board[move] = 5;
  251.               strategy = 7;
  252.               return ;
  253.             }
  254.               }
  255.             }
  256.           }
  257.         }
  258.               }
  259.             }
  260.       }
  261.     }
  262.       }
  263.     }
  264.       
  265.     if (diag) printf("7");
  266.    
  267.  
  268.   /* For computer */
  269.   /* Check for a row with 2 intersecting
  270.      a row with 0 intersecting a different row with 2
  271.      with blank squares as intersecting squares   
  272.      take a non-intersecting square in the 0 row */
  273.   for (i=0;i<sum_count[C][2]-1;i++)
  274.   {
  275.     for (j=i+1;j<sum_count[C][2];j++)
  276.     {
  277.       /* 2 rows - 2 in row */
  278.       k = sum_list[C][2][i] ; /* win_square index */
  279.       l = sum_list[C][2][j] ; /* win_square index */
  280.       for (m=0;m<sum_count[P][0];m++)
  281.       {
  282.     /* a row - 0 in row */
  283.     /* now find 2 empty intersecting squares */
  284.     n = sum_list[P][0][m] ; /* win_square index */
  285.         /* n is row with 0 in a row */
  286.     for (o=0;o<4;o++)
  287.     {
  288.       if (board [ win_square[n][o] ] ==0 )
  289.       {  /* square empty */
  290.         for (p=0;p<4;p++)
  291.         {
  292.           if (win_square[n][o]== win_square[l][p] )
  293.           { /* 2 intersects 1 in blank square */
  294.         for (q=0;q<4;q++)
  295.         {
  296.           if (board [ win_square[n][q] ] ==0 && q != p )
  297.           {  /* square empty in 1 in row */
  298.             for (r=0;r<4;r++)
  299.             {
  300.               if (win_square[n][q] == win_square[k][r] )
  301.               {
  302.              move1 = win_square[k][r] ;
  303.                          /* 2nd intersecting square */
  304.              move2 = win_square[l][p] ;
  305.                          /* 1st intersecting square */
  306.                          for (s = 0; s<4 ; s++)
  307.                          {
  308.                            move = win_square[n][s] ;
  309.                            if ( move != move1 && move != move2 )
  310.                            {
  311.                              board[move] = 5;
  312.                              strategy = 6;
  313.                              return ;
  314.                            }
  315.                          }
  316.                }
  317.              }
  318.            }
  319.          }
  320.            }
  321.              }
  322.            }
  323.      }
  324.        }
  325.      }
  326.    }
  327.    
  328.  
  329.     start=rand()/2048 ; /* get random number from 0 to 15 */
  330.     if (start<0 || start>15)
  331.       printf("Random number error./n");
  332.  
  333.     for (k=start;k<16;k++)
  334.     /* Computer takes move from major diagonal */
  335.       {
  336.         i=k/4;
  337.         j=k-i*4;
  338.     if ( board[ win_square[i][j] ] == 0 )